git cheatsheet
Terminology
Command | Description |
---|---|
repo | A place where the history of your work/code is stored |
clone | Copy a git repository so you can add to it |
fork (GitHub) | Create your own server side copy of someone else’s repo |
commit | A state of your project at a certain time |
branch | A different line of development. A branch is just a “label” which points to a specific commit |
master | The default branch we develop in |
origin | The default upstream repo (Github) |
HEAD | The current branch |
remote | A repository stored on another computer |
Starting a repo
Command |
Description |
---|---|
git init | Create a repo from existing data |
git clone <repo url> | Clone a current repo (into a folder with same name as repo) |
git clone <repo url> | <folder name> Clone a repo into a specific folder name |
git clone <repo url> | . Clone a repo into current directory (should be an empty directory) |
git remote add origin http//github.com/username/<repo name>.git |
Create a remote repo named origin pointing at your Github repo (after you’ve already created the repo on Github) (used if you git init since the repo you created locally isn’t linked to a remote repo yet) |
git remote add origin git@github.com:username/<repo name>.git |
Create a remote repo named origin pointing at your Github repo (using SSH url instead of HTTP url) |
git remote | Show the names of the remote repositories you’ve set up |
git remote -v | Show the names and URLs of the remote repositories |
git remote rm <remote name> | Remove a remote repository |
Showing/Undoing Changes
Command | Description |
---|---|
git status | Show the files changed |
git diff | Show changes to files compared to last commit |
git diff <filename> | Show changes in single file compared to last commit |
git diff <commit id > <commit id 2> | Show changes between two different commits. |
git log | Show history of changes |
git blame <filename> | Show who changed each line of a file and when |
git reset -hard | Go back to the last commit (will not delete new unstaged files) |
git revert HEAD | Undo/revert last commit AND create a new commit |
git revert <commit id> | Undo/revert a specific commit AND create a new commit |
Staging
Command |
Description |
---|---|
git add -A | Stage all files (new, modified, and deleted) |
git add . | Stage new and modified files (not deleted) |
git add -u | Stage modified and deleted files (not new) |
git rm <filename> | Remove a file and untrack it |
git rm <filename> -cached | Untrack a file only. It will still exist. Usually you will add this file to .gitignore after rm |
Publishing
Command |
Description |
---|---|
git commit -m “message” | Commit the local changes that were staged |
git commit -am “message” | Stage files (modified and deleted, not new) and commit |
git stash | Take the uncommitted work (modified tracked files and staged changes) and saves it |
git stash list | Show list of stashes |
git stash apply | Reapply the latest stashed contents |
git stash apply <stash id> | Reapply a specific stash. (stash id = stash@{2}) |
git stash drop <stash id> | Drop a specific stash |
git push | Push your changes to the origin |
git push origin <local branch name> | Push a branch to the origin |
git tag <tag name> | Tag a version (ie v1.0). Useful for Github releases |
Updating & Getting Code
Command |
Description |
---|---|
git | fetch |
git pull | Get the latest changes from origin AND merge |
git checkout -b |
Get a remote branch from origin into a local branch (naming the branch and switching to it) |
Branching
Command |
Description |
---|---|
git branch | Show all branches (local) |
git branch -a | Show all branches (local and remote) |
git branch <branch name> | Create a branch from HEAD |
git checkout -b <branch name> | Create a new branch and switch to it |
git checkout <branch name> | Switch to an already created branch |
git push origin <branch name> | Push a branch up to the origin (Github) |
git checkout -b |
Get a remote branch from origin into a local branch (naming the branch and switching to it) |
git push origin -delete <branch name> | Delete a branch locally and remotely |
Branching(Advanced)
Command |
Description |
---|---|
git checkout master git merge <branch name> |
Merge a specific branch into the master branch. |
git rebase <branch name> | Take all the changes in one branch and replay them on another. Usually used in a feature branch. Rebase the master to the feature branch so you are testing your feature on the latest main code base. Then merge to the master. |
git cherry-pick <commit id> | Merge just one specific commit from another branch to your current branch. |